﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using vkMessengerApp.Managers;

namespace vkMessengerApp.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        /// <summary>
        /// The <see cref="Username" /> property's name.
        /// </summary>
        public const string UsernamePropertyName = "Username";

        private string _username = string.Empty;

        /// <summary>
        /// Gets the Username property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Username
        {
            get
            {
                return _username;
            }

            set
            {
                if (_username == value) return;

                _username = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(UsernamePropertyName);
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        /// <summary>
        /// The <see cref="Password" /> property's name.
        /// </summary>
        public const string PasswordPropertyName = "Password";

        private string _password = string.Empty;

        /// <summary>
        /// Gets the Password property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }

            set
            {
                if (_password == value) return;
                _password = value;
                // Update bindings, no broadcast
                RaisePropertyChanged(PasswordPropertyName);
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        public void Login()
        {
            UserManager.Login(_username, _password);
        }

        public bool CanLogin()
        {
            return (!string.IsNullOrEmpty(Username)) && (!string.IsNullOrEmpty(Password));
        }

        public RelayCommand LoginCommand { get; private set; }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public LoginViewModel()
        {
            LoginCommand = new RelayCommand(Login, CanLogin);
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}